package com.geticsen.leetcode;

import java.util.*;
//https://leetcode-cn.com/problems/group-anagrams/
public class Solution_49 {

    public List<List<String>> groupAnagrams(String[] strs) {
        List<List<String>> result = new ArrayList<>();
        Map<String,List<String>> group = new HashMap<>();
        for (String str:strs){
            char[] chars = str.toCharArray();
            Arrays.sort(chars);
            String key = new String(chars);
            if (group.get(key)==null){
                List<String> list = new ArrayList<>();
                list.add(str);
                group.put(key,list);
            }else {
                group.get(key).add(str);
            }
        }
        for (Map.Entry<String,List<String>> entry:group.entrySet()){
            result.add(entry.getValue());
        }
        return result;
    }
}
